package com.otod.sys.org.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.commons.web.BaseController;
import com.otod.sys.org.bo.OrgSaveBO;
import com.otod.sys.org.bo.OrgTreeBO;
import com.otod.sys.org.mapper.OrgMapper;
import com.otod.sys.org.service.IOrgService;
import com.otod.sys.vo.AuapTOrgVo;

/**
 * 组织管理模块controller
 * @ClassName: OrgController
 * @author jqiu
 * @date 2014年2月19日 上午10:31:09
 *
 */
@Controller
@RequestMapping("sys/org")
public class OrgController extends BaseController{
	@Autowired
	private IOrgService orgService;
	
	@Autowired
	private OrgMapper orgMapper;
	
//	@RequestMapping(value = "findAll", method = RequestMethod.POST)
//	@ResponseBody
//	public List<AuapTOrgVo> findAll(HttpServletRequest request,
//			HttpServletResponse response) throws Exception {
//		List<AuapTOrgVo> list = orgService.findAll();
//		return list;
//	}
	
	@RequestMapping(value = "findAllTree", method = RequestMethod.POST)
	@ResponseBody
	public List<OrgTreeBO> findAllTree(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<OrgTreeBO> list = orgMapper.findAllTree();
		return list;
	}
	
	@RequestMapping(value = "findAsyncTree", method = RequestMethod.POST)
	@ResponseBody
	public List<OrgTreeBO> findAsyncTree(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String orgNo = request.getParameter("orgNo");
		Map map = new HashMap();
		map.put("orgNo", orgNo);
		List<OrgTreeBO> list = orgMapper.findAsyncTree(map);
		return list;
	}
	
//	@RequestMapping(value = "findDatatables", method = RequestMethod.POST)
//	@ResponseBody
//	public DataTablesData<AuapTOrgVo> findDataTables(HttpServletRequest request) {
//		Map map = super.getParamMapFromRequest(request);
//		DataTablesData<AuapTOrgVo> result = new QueryHelper<AuapTOrgVo>()
//				.prepareDataTablesData(orgMapper, "findPage" ,
//						map);
//		return result;
//	}
	
	/**
	 * 检查组织编号是否重复
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "checkRepeat", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, String> checkRepeat(HttpServletRequest request)
			throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String orgNo = request.getParameter("orgNo");
		String orgId = request.getParameter("orgId");
		Long count = 0L;
		if(orgId == null || "".equals(orgId)) {
			count = orgMapper.isRepeatOrgNo(orgNo);
		} else {
			Map param = new HashMap();
			param.put("orgNo", orgNo);
			param.put("orgId", orgId);
			count = orgMapper.isRepeatOrgNoOrgId(param);
		}
		if (count != null && count >= 1) {
			map.put("isRepeat", "true");
		} else {
			map.put("isRepeat", "false");
		}
		return map;
	}
	
	/**
	 * 保存组织信息
	 * @param AuapTOrgVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> save(@RequestBody OrgSaveBO orgSaveBO) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String orgId = orgSaveBO.getUuid();
		if(orgId == null || "".equals(orgId)) {
			orgService.addOrg(orgSaveBO);
		} else {
			orgService.eidtOrg(orgSaveBO);
		}
		return map;
	}
	
	/**
	 * 获取组织信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getOrgInfo", method = RequestMethod.GET)
	@ResponseBody
	public Map getOrgInfo(HttpServletRequest request) throws Exception {
		Map map = new HashMap();
		String orgNo = request.getParameter("orgNo");
		OrgSaveBO orgSaveBO = new OrgSaveBO();
		AuapTOrgVo AuapTOrgVo = orgService.getByOrgNo(orgNo);
		BeanUtils.copyProperties(AuapTOrgVo, orgSaveBO);
		AuapTOrgVo parentOrg = orgService.getByOrgNo(AuapTOrgVo.getParentOrgId());
		map.put("orgSaveBO", orgSaveBO);
		map.put("parentOrg", parentOrg);
		return map;
	}
	
	@RequestMapping(value = "findOrgRoleMenu", method = RequestMethod.GET)
	@ResponseBody
	@Deprecated
	public List<String> findOrgRoleMenu(HttpServletRequest request) throws Exception {
		 return null;
	 }
	
//	@RequestMapping(value = "saveOrgRoleMenu", method = RequestMethod.POST)
//	@ResponseBody
//	public Map<String, String> saveOrgRoleMenu(HttpServletRequest request, @RequestBody String[] menuIds) throws Exception {
//		Map<String, String> map = new HashMap<String, String>();
//		String orgId = request.getParameter("orgId");
//		String roleId = request.getParameter("roleId");
//		orgService.setOrgRoleMenu(orgId, roleId, menuIds);
//		return map;
//	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Map delete(HttpServletRequest request) {
		Map map = new HashMap();
		String orgId= request.getParameter("orgId");
		try {
			orgService.delOrg(orgId);
		} catch (Exception e) {
			map.put("msg", e.getMessage());
			e.printStackTrace();
		}
		return map;
	}
	
	
}
